<?php
include 'su_log.php';
include '../inc/d.php';
$rows = query('hw_banner');
//删除
if (isset($_GET['id']) && isset($_GET['filename'])) {
    deleteById('hw_banner', $_GET['id']);
    unlink('../upload/banner/' . $_GET['filename']);
    header('location:./');
}
//修改flag
if (isset($_GET['id']) && isset($_GET['flag'])) {
    $arr_assoc['banner_id'] = $_GET['id'];
    $arr_assoc['banner_flag'] = $_GET['flag'];
    updateByPID('hw_banner', $arr_assoc);
    header('location:./');
}
//修改广告名称
if (isset($_GET['id']) && isset($_GET['name'])) {
    $arr_assoc['banner_id'] = $_GET['id'];
    $arr_assoc['banner_name'] = $_GET['name'];
    updateByPID('hw_banner', $arr_assoc);
    header('location:./');
}
//修改链接地址
if (isset($_GET['id']) && isset($_GET['url'])) {
    $arr_assoc['banner_id'] = $_GET['id'];
    $arr_assoc['banner_name'] = $_GET['url'];
    updateByPID('hw_banner', $arr_assoc);
    header('location:./');
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>admin</title>
    <script src="../js/jquery-3.1.1.min.js"></script>
    <style>
        .tb {
            border-collapse: collapse;
            margin: 0 auto;
        }

        .tb tr td, .tb tr th {
            padding: 8px;
            border: 1px solid gray;
        }

        .yes {
            opacity: 1;
        }

        .no {
            opacity: .5;
        }
    </style>
</head>
<body>
<table class="tb">
    <tr>
        <th>ID</th>         <!--1-->
        <th>广告名称</th>    <!--2-->
        <th>广告文件</th>    <!--3-->
        <th>广告图片</th>    <!--4-->
        <th>url</th>        <!--5-->
        <th>是否显示</th>    <!--6-->
        <th>基本操作</th>    <!--7-->
    </tr>
    <?php foreach ($rows as $value) { ?>
    <tr>
        <!--ID--><!--1-->
        <td><?= $value['banner_id'] ?></td>
        <!--广告名称--><!--2-->
        <td>
            <!--修改广告名称-->
            <?php if (isset($_GET['id']) && $value['banner_id'] == $_GET['id']){ ?>
            <form action="" method="get">
                <input type="hidden" name="id" value="<?= $value['banner_id'] ?>">
                <input type="text" name="name" value="<?= $value['banner_name'] ?>">
                <input type="submit" value="保存">
                <?php } else { ?>
                    <?= $value['banner_name'] ?>
                <?php } ?>
            </form>
        </td>
        <!--广告文件--><!--3-->
        <td><?= $value['banner_filename'] ?></td>
        <!--广告图片--><!--4-->
        <td><img width="50" height="50" src="../upload/banner/<?= $value['banner_filename'] ?>"></td>
        <!--url--><!--5-->
        <td>
            <!--修改URL-->
            <?php if (isset($_GET['id']) && $value['banner_id'] == $_GET['id']) { ?>
                <form>
                    <input type="hidden" name="id" value="<?= $value['banner_id'] ?>">
                    <input type="text" name="url" value="<?= $value['banner_url'] ?>">
                    <input type="submit" value="保存">
                </form>
            <?php } else { ?>
                <?= $value['banner_url'] ?>
            <?php } ?>
        </td>
        <!--是否显示--><!--6-->
        <td>
            <?php if ($value['banner_flag'] == 'y') { ?>
                <label class="yes"><input type="checkbox"
                                          onclick="location.href='?id=<?= $value['banner_id'] ?>&flag=n'"
                                          checked>显示</label>
            <?php } else { ?>
                <label class="no"><input type="checkbox"
                                         onclick="location.href='?id=<?= $value['banner_id'] ?>&flag=y'">显示</label>
            <?php } ?>
        </td>
        <!--基本操作--><!--7-->
        <td>
            <a href="?id=<?= $value['banner_id'] ?>&filename=<?= $value['banner_filename'] ?>">删除</a>&nbsp;
            <a href="?id=<?= $value['banner_id'] ?>">编辑</a>
        </td>
    </tr>

    <tr>
        <td>
            <?php
            if (isset($_GET['add'])){
            if (isset($POST['name'])) {
                if (empty(trim($_POST['name']))) {
                    echo '请输入广告名称';
                } elseif (empty($_FILES)) {
                    echo '请选择上传图片';
                } else {
                    $filename = date('YmdHis') . '.jpg';
                    move_uploaded_file($_FILES['file']['tmp_name'], '../upload/banner/' . $filename);
                    $arr_assoc['banner_name'] = $_POST['name'];
                    $arr_assoc['banner_filename'] = $filename;
                    $arr_assoc['banner_time'] = time();
                    $arr_assoc['banner_flag'] = $_POST['flag'];
                    $arr_assoc['banner_url'] = $_POST['url'];
                    save('hw_banner', $arr_assoc);
                    header('location:./');
                }
            }
            ?>
            <h3>添加广告</h3>
            <form action="" method="post" enctype="multipart/form-data">
                广告名称：<input type="text" name="name" size="80" autofocus><br>
                广告图片：<input type="file" name="file">(格式为jpeg (1000*277))<br>
                链接地址：<input type="text" name="url" value="#"><br>
                是否显示：
                <label><input type="radio" name="flag" value="y" checked>显示</label>&nbsp;
                <label><input type="radio" name="flag" value="n">不显示</label><br>
                <hr>
                <input type="submit" value="提交">
            </form>
        </td>


        <?php } else { ?>
            <td colspan="7"><a href="?add=1">添加轮播展示</a></td>
        <?php } ?>
        <?php } ?>
    </tr>
</table>
</body>
</html>